package foo.bar.wiki.web.action.resource;

import foo.bar.wiki.web.action.BaseActionBean;
import foo.bar.wiki.web.action.page.EditPageActionBean;
import foo.bar.wiki.web.action.page.PageActionBean;
import foo.bar.wiki.web.extension.ResourceTypeConverter;
import foo.bar.wiki.domain.Resource;
import foo.bar.wiki.services.WikiService;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.integration.spring.SpringBean;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class RemoveResourceActionBean extends BaseActionBean {

    @Validate(converter = ResourceTypeConverter.class)
    private Resource resource;


    private WikiService wikiService;


    @DefaultHandler
    public Resolution remove_backToEditPage() {
        String pageId = resource.getPage().getId();
        _remove();
        return new RedirectResolution(EditPageActionBean.class, "view")
                   .addParameter("currentPage", pageId);
    }

    public Resolution remove_backToPageWikiPage() {
        String pageId = resource.getPage().getId();
        _remove();
        return new RedirectResolution(PageActionBean.class, "view")
                    .addParameter("page", pageId);
    }


    private void _remove() {
        wikiService.removeResource(resource);
        getContext().getMessages().add(new SimpleMessage("Resource ["+resource.getName()+"] has been removed successfully"));
    }


    @SpringBean("wikiService")
    public void setWikiService(WikiService wikiService) {
        this.wikiService = wikiService;    
    }

    public Resource getResource() {
        return resource;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }
}
